package com.yehun.filter;

import java.io.IOException;
import java.util.Random;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GlobalFilter implements Filter {

    private FilterConfig config;
    private static final String baseUrlStatic = "baseUrlStatic";
    private static final String baseUrlAdmin = "baseUrlAdmin";
    private static final String baseUrlRoot= "baseUrlRoot";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.config = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        request.setAttribute(baseUrlStatic, config.getInitParameter(baseUrlStatic));
        request.setAttribute(baseUrlAdmin, config.getInitParameter(baseUrlAdmin));
        request.setAttribute(baseUrlRoot, config.getInitParameter(baseUrlRoot));
        //response.setHeader("Access-Control-Allow-Origin", "*");
        filterChain.doFilter(request, servletResponse);
    }

    @Override
    public void destroy() { }

}
